package com.cloud.base;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cloud.cache.user.CloudUserUtils;
import com.cloud.domain.file.Folder;
import com.cloud.service.file.FolderService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseFileController {

    @Autowired
    FolderService folderService;


    public Collection<Folder> ListChildrenFile(Long fileId) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("parent_id", fileId);
        Collection<Folder> folders = folderService.listByMap(queryMap);
        return folders;
    }

    public List<Folder> ListParentFile() throws Exception {
        Folder queryFolderParam = new Folder();
        queryFolderParam.setUserId(1l);
        queryFolderParam.setParentId(0L);
        QueryWrapper<Folder> queryWrapper = new QueryWrapper<>(queryFolderParam);
        List<Folder> list = folderService.list(queryWrapper);
        return list;
    }


    public Boolean isFileExistsById(Long id) throws Exception {
        if(id == null){
            return null;
        }
        Folder queryFolderParam = new Folder();
        queryFolderParam.setUserId(CloudUserUtils.getUserId());
        queryFolderParam.setId(id);
        return isFolderExists(queryFolderParam);

    }

    public boolean isFileExistsByNameOrParentName(Long parentId, String name) throws Exception {
        Folder queryFolderParam = new Folder();
        if (parentId == null) {
            queryFolderParam.setUserId(CloudUserUtils.getUserId());
            queryFolderParam.setParentId(0L);
            queryFolderParam.setName(name);
            return isFolderExists(queryFolderParam);

        } else {
            queryFolderParam.setUserId(CloudUserUtils.getUserId());
            queryFolderParam.setParentId(parentId);
            queryFolderParam.setName(name);
            return isFolderExists(queryFolderParam);

        }
    }

    public boolean isFolderExists(Folder queryFolderParam) {
        QueryWrapper<Folder> queryWrapper = new QueryWrapper<>(queryFolderParam);
        Folder folder = folderService.getOne(queryWrapper);
        if (folder == null) {
            return false;
        } else {
            return true;
        }
    }


}
